package nowcode;

import java.util.ArrayList;

public class FindNumbersWithSumSolution {


    public static void main(String[] args) {

    }


    public ArrayList<Integer> FindNumbersWithSum(int[] array, int sum) {

        ArrayList<Integer> list = new ArrayList<>();

        if (array.length <= 1) {
            return list;
        }
        int beginIndex = 0;
        int endIndex = array.length - 1;

        while (beginIndex < endIndex) {

            if (array[beginIndex] + array[endIndex] == sum) {
                list.add(array[beginIndex]);
                list.add(array[endIndex]);
                break;
            } else if (array[beginIndex] + array[endIndex] > sum) {
                endIndex--;
            } else {
                beginIndex++;
            }
        }
        return list;
    }
}
